package com.easemob.helpdesk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.easemob.helpdesk.bean.TagListItemBean;
import com.easemob.helpdesk.widget.SwipeItemLayout;
import com.housekeeperapp.R;

import java.util.List;



/**
 * Created by xiaolong.wei on 2019/4/3.
 */

public class TagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TITLE = 0;
    private static final int CONTENT = 1;

    private OnDeleteItemListener listener;


    private List<TagListItemBean> mTags;
    private Context mContext;
    private LayoutInflater mInflater;

    public TagAdapter(Context mContext,List<TagListItemBean> listItemBean) {
        this.mContext = mContext;
        this.mTags = listItemBean;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TITLE){
            View titleView = mInflater.inflate(R.layout.tag_title, parent, false);
            return new TitleViewHolder(titleView);
        }else if (viewType == CONTENT){
            View contentView = mInflater.inflate(R.layout.tag_item, parent, false);
            return new ContentViewHolder(contentView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vh, final int position) {

        if (vh instanceof TitleViewHolder){
            TitleViewHolder holder = (TitleViewHolder) vh;
            holder.title.setText(mTags.get(position).getContent());
        }else {
            ContentViewHolder holder = (ContentViewHolder) vh;
            holder.tag.setText(mTags.get(position).getContent());
            if (listener != null){
                holder.deleteItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onDeleteItem(position,v);
                    }
                });
            }



        }
    }

    @Override
    public int getItemCount() {
        return mTags.size();
    }



    @Override
    public int getItemViewType(int position) {
        if ("title".equals(mTags.get(position).getType())){
            return TITLE;
        }else {
            return CONTENT;
        }
//        return super.getItemViewType(position);
    }

    public void setListener(OnDeleteItemListener listener) {
        this.listener = listener;
    }

    static class TitleViewHolder extends RecyclerView.ViewHolder{

        TextView title;

        public TitleViewHolder(View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.tag_title);
        }
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder{

        TextView tag;
        TextView deleteItem;
        SwipeItemLayout swipeItemLayout;
        public ContentViewHolder(View itemView){
            super(itemView);
            tag=itemView.findViewById(R.id.tag);
            deleteItem=itemView.findViewById(R.id.delete_item);
            swipeItemLayout=itemView.findViewById(R.id.swipe_layout);
        }

    }

    public interface OnDeleteItemListener{
        public void onDeleteItem(int position,View view);
    }
}
